import Vue from 'vue'
import VueRouter from 'vue-router'
import TotalView from '../views/TotalView.vue'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'total',
    component: TotalView,
    redirect: () => '/jobs',
    children: [{
        path: "jobs",
        name: "jobs",
        component: HomeView
      },
      {
        path: "company",
        name: "company",
        component: () => import("@/views/CompanyView.vue")
      },
      {
        path: "message",
        name: "message",
        meta: {
          shouldLogin: true
        },
        component: () => import("@/views/MessageView.vue")
      },
      {
        path: "mine",
        name: "mine",
        meta: {
          shouldLogin: true
        },
        component: () => import("@/views/MineView.vue")
      },
    ]
  }, 
  //登录界面
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/LoginView.vue")
  },
  //岗位详情
  {
    path: "/jobdetail",
    name: "jobdetail",
    component: () => import("@/views/JobView.vue")
  },
  //公司详情
  {
    path: "/companydetail",
    name: "companydetail",
    component: () => import("@/views/CompanyDetailView.vue")
  },
  //聊天界面
  {
    path: "/chat",
    name: "chat",
    component: () => import("@/views/ChatView.vue")
  },
  //收藏页面
  {
    path: "/collect",
    name: "collect",
    component: () => import("@/views/CollectView.vue")
  },
  //在线简历
  {
    path: "/resume",
    name: "resume",
    component: () => import("@/views/ResumeView.vue")
  },
  //简历列表
  {
    path: "/resumelist",
    name: "resumelist",
    component: () => import("@/views/ResumeListView.vue")
  },
]
const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to?.meta?.shouldLogin) {
    let token = localStorage.getItem("token");
    if (!token) {
      next({
        path: "/login",
        name: "login"
      });
    } else {
      //验证token,获取用户信息
      window.axios({
        method: "post",
        url: "/v1/user/isValidateToken",
        headers: {
          Authorization: "Bearer " + token
        }
      }).then((res) => {
        if (res.data.statusCode == 200) {
          next();
        } else next({
          path: "/login",
          name: "login"
        });
      })
    }
  } else {
    next();
  }
})
export default router